package com.design.movieticket.login.controller;

import cn.hutool.core.util.RandomUtil;
import com.design.movieticket.common.base.constant.Result;
import com.design.movieticket.common.base.constant.ResultStatus;
import com.design.movieticket.common.util.JwtUtils;
import com.design.movieticket.entity.User;
import com.design.movieticket.login.entity.LoginUser;
import com.design.movieticket.login.service.LoginService;
import com.design.movieticket.login.service.LogoutService;
import com.design.movieticket.login.service.RedisService;
import com.design.movieticket.login.service.SendEmailService;
import io.jsonwebtoken.Claims;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;


@RestController
@RequestMapping("/user")
@Slf4j
@Api(tags = "后台登录模块")
public class LoginController {

    @Autowired
    private LoginService loginService;

    @Autowired
    private LogoutService logoutService;

    @Resource
    private SendEmailService sendEmailService;

    @Resource
    RedisService redisService;

    String checkNumber = "123456";

    @ApiOperation("登录")
    @PostMapping("/login")
    public Result login(@RequestBody User user){
        log.info("访问了登录接口start");
        log.info("前端接收的User:"+ user);


        //登录
        Result login = loginService.login(user);
        log.info("访问了登录接口end");

        return login;
    }


    @ApiOperation("登出")
    @GetMapping("/logout")
    public Result logout(){
        log.info("访问了注销接口start");

        Result result = logoutService.logout();

        log.info("访问了注销接口end");
        return result;
    }

    @ApiOperation("登录信息")
    @GetMapping("/info")
    public Result info(String token){
        log.info("登录信息获取的token是");
        //token解析
        Claims claims = JwtUtils.verifyJwt(token);
        String id = String.valueOf(claims.get("id"));
        String redisKey = "login" + id;
        log.info("解析得到的redisKey:" + redisKey);

        //从redis中读取信息
        LoginUser loginUser = (LoginUser) redisService.get(redisKey);
        log.info("解析得到的用户:" + loginUser);
        if (loginUser == null){
            throw new RuntimeException("用户未登录");
        }
        return new Result(loginUser,"信息",20000);
    }

    @GetMapping("/getCheckNumber")
    public void getCheckNumber(){
        checkNumber = RandomUtil.randomNumbers(6);
        sendEmailService.sendEmail(checkNumber);
    }

    @ApiOperation("用户注册")
    @PostMapping("/register/{checkNum}")
    public Result register(@RequestBody User user,@PathVariable String checkNum){
        System.out.println(user + " " + checkNum + " " + checkNumber);
        if (checkNumber == null){
            return new Result(ResultStatus.REGISTER_FAIL.getMsg(),ResultStatus.REGISTER_FAIL.getCode());
        }
        if (checkNumber.equals(checkNum)){
            //注册
            Result login = loginService.register(user);
            return login;
        }else{
            return new Result(ResultStatus.REGISTER_FAIL.getMsg(),ResultStatus.REGISTER_FAIL.getCode());
        }

    }
}
